﻿<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <Result Status="404"
                    Title="资源未找到（404）"
                    Icon=""
                    SubTitle="对比起，网页已经去喵星球了" />
        </LayoutView>
    </NotFound>
</Router>
<BackTop></BackTop>
<AntContainer />
@inject Blazored.LocalStorage.ISyncLocalStorageService _localStorage
@inject HttpClient _httpClient
@inject NotificationService _notice
@code {
    protected async override Task OnInitializedAsync() {
        Console.WriteLine("App初始化");
        if (GobalManager.myUsrInfo == null && _localStorage.ContainKey("unameID")) {
            try {
                var resp = await GobalManager.GetUsrInfoById(_localStorage.GetItem<int>("unameID"));
                if (resp != null) {
                    GobalManager.myUsrInfo = resp;
                    GobalManager.dCusNVCD?.Invoke();
                    await NoticeWithIcon(NotificationType.Success, "自动登录成功", "自动登录成功。");
                } else {
                    await NoticeWithIcon(NotificationType.Error, "自动登录失败了", "自动登录失败，可能是用户删除了。也可能是后端服务器没有打开。", 0);
                }
            }
            catch (Exception ex) {
                Console.WriteLine("自动登录失败了！" + ex.Message);
                await NoticeWithIcon(NotificationType.Error, "自动登录失败了", "自动登录失败，可能是用户删除了。也可能是后端服务器没有打开。", 0);
            }
        }
        await base.OnInitializedAsync();
    }

    public async Task NoticeWithIcon(NotificationType type, string title, string body, double durtion = 4.5) {
        await _notice.Open(new NotificationConfig() {
                Message = title,
                Description = body,
                NotificationType = type,
                Duration = durtion
            });
    }
}